import 'package:flutter/material.dart';

class ExpansionPanelListDemo extends StatefulWidget {
  @override
  _ExpansionPanelListDemoState createState() => _ExpansionPanelListDemoState();
}

class _ExpansionPanelListDemoState extends State<ExpansionPanelListDemo> {
  List<int> _mlist;
  List<ExpandStateBean> _expandStateList;

  _ExpansionPanelListDemoState() {
    _mlist = new List();
    _expandStateList = new List();
    for (int i = 0; i < 10; i++) {
      _mlist.add(i);
      _expandStateList.add(ExpandStateBean(i, false));
    }
  }

  _setCurrentExpandIndex(int index, bool isExpand) {
    setState(() {
      _expandStateList.forEach((item) {
        if (index == item.index) {
          item.isOpen = !isExpand;
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ExpandPanelList'),
      ),
      body: SingleChildScrollView(
        child: ExpansionPanelList(
          expansionCallback: (index, isExpand) {
            _setCurrentExpandIndex(index, isExpand);
          },
          children: _mlist.map(
            (index) {
              return ExpansionPanel(
                headerBuilder: (context, isExpand) {
                  return ListTile(
                    title: Text('This is No.$index'),
                  );
                },
                body: ListTile(
                  title: Text('this is no.$index'),
                ),
                isExpanded: _expandStateList[index].isOpen,
              );
            },
          ).toList(),
        ),
      ),
    );
  }
}

class ExpandStateBean {
  //记录列表是否打开的状态
  var isOpen;
  var index;

  ExpandStateBean(this.index, this.isOpen);
}
